<template>
  <div class="main">
    <el-header>
      <div class="header-leftbox">
        <img :src="avaterImg" alt="" />
        <span>我的收藏</span>
      </div>
      <div class="header-right">
        <el-button type="info" @click="logout">返回首页</el-button>
      </div>
    </el-header>
    <div class="favoriteMain">
      <div class="myorder">
        <h3>我的收藏</h3>
      </div>
      <div class="favoriteList-box">
        <div class="listTitle">
          <div class="menuBox">
            <el-menu
              :default-active="activeIndex"
              mode="horizontal"
              @select="handleSelect"
              :ellipsis="false"
            >
              <el-menu-item index="1" @click="activeIndex = 1"
                >收藏的商品</el-menu-item
              >
              <el-menu-item index="2" @click="activeIndex = 2"
                >收藏的店铺</el-menu-item
              >
              <el-menu-item index="3" @click="enterACtivityPage"
                >收藏的活动</el-menu-item
              >
            </el-menu>
          </div>
          <div class="searchBox">
            <el-input v-model="input" placeholder="请输入商品名称">
              <template #append>
                <el-button :icon="Search" />
              </template>
            </el-input>
          </div>
        </div>
        <!-- 收藏商品 -->
        <div class="listcontent" v-show="activeIndex == 1">
          <div class="empty" v-if="isEmpty">
            <el-empty description="你还没有关注任何商品哦" />
          </div>
          <div
            class="faGoodList-box"
            v-for="(item, index) in favoriteGoodsList"
            :key="item"
          >
            <div class="btn-box">
              <el-button @click="cancelLove(item.serialNo)">取消收藏</el-button>
              <el-button
                v-if="item.toppingFlag === 'N'"
                @click="putFrist(item.serialNo)"
                >置顶</el-button
              >
              <el-button
                v-if="item.toppingFlag === 'Y'"
                @click="exitFrist(item.serialNo)"
                >取消置顶</el-button
              >
            </div>
            <div class="setMargin" v-if="item.toppingFlag === 'Y'">
              <i class="iconfont icon-zhiding"></i>
            </div>
            <div class="img-box" @click="viewDetail(item.objectNo)">
              <img :src="item.defaultImage" alt="" />
            </div>
            <div class="text-box">
              <div class="introduce">
                <p
                  :title="item.spuName"
                  :alt="item.spuName"
                  @click="viewDetail(item.objectNo)"
                >
                  {{ item.spuName }}
                </p>
              </div>
              <div class="price">¥{{ item.price }}.00</div>
              <el-button @click="joinMyCart(item.spuNo)">加入购物车</el-button>
              <div class="listIndex">{{ index + 1 }}</div>
            </div>
          </div>
        </div>
        <!-- 收藏店铺 -->
        <div class="listcontent" v-show="activeIndex == 2">
          <div class="empty" v-if="isEmpty2">
            <el-empty description="你还没有关注任何商铺哦" />
          </div>
          <div
            class="fashopList-box"
            v-for="(item, index) in favoriteShopsList"
            :key="item.id"
          >
            <div class="btn-box">
              <el-button @click="cancelShopLove(item)">取消收藏</el-button>
              <el-button @click="putShopFrist(item)">置顶</el-button>
            </div>
            <div class="shopIcon-box">
              <img :src="item.shopIcon" alt="" />
            </div>
            <div class="shoptext-box">
              <h3>{{ item.shopName }}</h3>
            </div>
            <div class="rate">
              <span>店铺星级</span>
              <star-view :score="item.shopRate"></star-view>
            </div>
            <div class="joinCartBtn">
              <el-button>进入店铺</el-button>
              <div class="shopListIndex">{{ index + 1 }}</div>
            </div>
          </div>
        </div>
        <!-- 收藏活动 -->
        <div class="listcontent" v-show="activeIndex == 3">
          <div class="empty" v-if="isEmpty3">
            <el-empty description="你还没有关注任何活动哦" />
          </div>
          <div
            class="faGoodList-box"
            v-for="item in favoriteActiveList"
            :key="item.id"
          >
            <div class="btn-box">
              <el-button @click="cancelLove(item.serialNo)">取消收藏</el-button>
              <el-button>置顶</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <footer-view></footer-view>
  </div>
</template>

<script>
import { Search } from "@element-plus/icons-vue";
import DisImg5 from "../../assets/image/shopIconDemo.jpg";
import avaterImg from "../../assets/image/avaterDemo.jpg";
import {
  collectionInfoList,
  removeCollectionInfo,
  setTopCollectionInfo,
  saveCartInfo,
  backTopCollectionInfo,
} from "@/api/user";
import { queryGoodsInfo } from "@/api/goods";
export default {
  data() {
    return {
      avaterImg: avaterImg,
      activeIndex: "1",
      favoriteGoodsList: [],
      favoriteShopsList: [
        { shopIcon: DisImg5, shopName: "林泽屋家具旗舰店", shopRate: "3" },
      ],
      favoriteActiveList: {},
      spuInfoList: [],
      defaultSkuList: {},
      serialNo: "",
      cartList: {
        skuNo: "",
        quantity: 0,
      },
    };
  },
  mounted() {
    this.getCollectionInfoList();
    this.init();
  },
  computed: {
    isEmpty() {
      if (this.favoriteGoodsList.length > 0) {
        return false;
      }
      return true;
    },
    isEmpty2() {
      if (this.favoriteShopsList.length > 0) {
        return false;
      }
      return true;
    },
    isEmpty3() {
      if (this.favoriteActiveList.length > 0) {
        return false;
      }
      return true;
    },
  },
  setup() {
    return {
      Search,
    };
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/home");
    },
    // 取消关注商品
    cancelLove(serialNo) {
      let msg = "你确定要取消关注该商品吗?";
      // 弹出一个框，用来提示用户是否确定删除
      this.$confirm(msg, "提示", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning",
      })
        .then(() => {
          removeCollectionInfo([serialNo]).then((res) => {
            if (res.code === 0) {
              this.$message.success("取消收藏成功");
              this.getCollectionInfoList();
            }
          });
        })
        .catch(() => {
          console.log("cancel");
        });
    },
    // 取消店铺收藏
    cancelShopLove(event) {
      let pos = 0;
      let msg = "你确定要取消关注该商品吗?";
      // 弹出一个框，用来提示用户是否确定删除
      this.$confirm(msg, "提示", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning",
      })
        .then(() => {
          pos = this.favoriteShopsList.indexOf(event);
          this.favoriteShopsList.splice(pos, 1);
          this.$message.success("已取消收藏");
        })
        .catch(() => {
          console.log("cancel");
        });
    },
    // 置顶
    putFrist(serialNo) {
      console.log(serialNo);
      setTopCollectionInfo(serialNo).then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.$message.success("置顶成功");
          this.getCollectionInfoList();
        }
      });
    },
    // 取消置顶
    exitFrist(serialNo) {
      backTopCollectionInfo(serialNo).then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.$message.success("取消置顶成功");
          this.getCollectionInfoList();
        }
      });
    },
    putShopFrist(event) {
      let pos = this.favoriteShopsList.indexOf(event);
      console.log(pos);
      // 将置顶元素置于数组第一个
      if (pos) {
        let first = this.favoriteShopsList.splice(pos, 1)[0];
        this.favoriteShopsList.unshift(first);
      }
    },
    // 获取商品收藏列表
    getCollectionInfoList() {
      collectionInfoList({ objectType: "goods" }).then((res) => {
        console.log(res);
        this.favoriteGoodsList = res.page.list;
      });
    },
    // 获取获得收藏列表
    getAcitveInfoList() {
      collectionInfoList({ objectType: "activity" }).then((res) => {
        console.log(res);
        this.favoriteShopsList = res.page.list;
      });
    },
    enterACtivityPage() {
      this.activeIndex = 3;
      this.getAcitveInfoList();
    },
    init() {
      let userInfo = this.$store.getters.getUserInfo;
      console.log(userInfo);
      if (userInfo.avatar) {
        this.avaterImg = userInfo.avatar;
      }
    },
    viewDetail(serialNo) {
      this.$router.push({
        name: "commodity",
        params: {
          spuNo: serialNo,
        },
      });
    },
    joinMyCart(spuNo) {
      queryGoodsInfo(spuNo).then((res) => {
        console.log(res);
        this.spuInfoList = res.spuInfo;
        this.defaultSkuList = this.spuInfoList.skuList[0];
        this.serialNo = this.defaultSkuList.serialNo;
        let cartList = {
          skuNo: this.serialNo,
          quantity: 1,
        };
        this.cartList = cartList;
        console.log(this.cartList);
        saveCartInfo(cartList).then((res) => {
          console.log(res);
          if (res.code === 0) {
            this.$message.success("成功加入到的我的购物车");
          }
        });
      });
    },
  },
};
</script>

<style lang="scss">
.main {
  background-color: #f5f5f5;
  .el-header {
    background-color: #f6fbf6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 70px;
    .header-leftbox {
      display: flex;
      align-items: center;
      img {
        height: 30px;
        width: 30px;
        margin: 0 20px;
        border-radius: 50%;
        border: 1px solid #244d4d;
        box-shadow: 1px 1px #ededf1;
      }
      span {
        color: #244d4d;
        font-size: 30px;
        font-weight: bold;
      }
    }
    .header-right {
      display: flex;
      align-items: center;
      .el-button {
        font-weight: bold;
        background-color: #244d4d;
      }
    }
  }
  .favoriteMain {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    .myorder {
      margin: 20px 0;
      padding: 0 30px;
      width: 1210px;
      height: 80px;
      background-color: #fff;
      display: flex;
      align-items: center;
      h3 {
        font-size: 15px;
        color: #666;
        line-height: 18px;
      }
    }
    .favoriteList-box {
      position: relative;
      background-color: #fff;
      width: 1210px;
      display: flex;
      flex-direction: column;
      padding: 20px 20px 200px 20px;
      align-items: center;
      .listTitle {
        display: flex;
        width: 100%;
        justify-content: space-between;
        align-items: center;
        .searchBox {
          .el-input__inner {
            width: 200px;
            border-radius: 0;
          }
        }
      }
      .listcontent {
        display: flex;
        padding: 20px;
        justify-content: flex-start;
        width: 100%;
        flex-wrap: wrap;
        .empty {
          display: flex;
          width: 100%;
          justify-content: center;
          align-items: center;
        }
        .faGoodList-box {
          position: relative;
          width: 261px;
          height: 336px;
          border: 1px solid #dfe2e8;
          margin: 0 20px 20px 0;
          overflow: hidden;
          display: flex;
          flex-direction: column;
          padding-top: 24px;
          &:hover {
            .btn-box {
              top: 0;
            }
          }
          .btn-box {
            display: flex;
            position: absolute;
            top: -24px;
            left: 0;
            .el-button {
              background: rgba(0, 0, 0, 0.5);
              color: #fff;
              width: 125px;
              border-radius: 0;
              height: 24px;
              line-height: 24px;
            }
          }
          .setMargin {
            position: absolute;
            top: -10px;
            i {
              font-size: 40px;
              color: #244d4d;
            }
          }
          .img-box {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            overflow: hidden;
            img {
              height: 160px;
              width: 160px;
              &:hover {
                transform: scale(1.1);
              }
            }
          }
          .text-box {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            .introduce {
              padding: 0 5px;
              width: 260px;
              text-align: center;
              margin: 20px 0 30px 0;
              p {
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
                font-size: 14px;
                font-weight: 700;
                cursor: pointer;
                color: #666;
                &:hover {
                  color: red;
                }
              }
            }
            .price {
              color: #333;
              font-family: verdana;
              font-weight: bold;
            }
            .el-button {
              margin-top: 10px;
              background-color: #f9f9f9;
              border-radius: 0;
              width: 265px;
              height: 45px;
              font-size: 16px;
              font-weight: bold;
              &:hover {
                background-color: #244d4d;
                color: #fff;
              }
            }
            .listIndex {
              width: 30px;
              height: 30px;
              position: absolute;
              bottom: 45px;
              left: 0;
              background-color: #f2f2f2;
              display: flex;
              align-items: center;
              justify-content: center;
              color: #333;
            }
          }
        }
        .fashopList-box {
          position: relative;
          width: 300px;
          height: 336px;
          border: 1px solid #dfe2e8;
          margin: 0 20px 20px 0;
          overflow: hidden;
          display: flex;
          flex-direction: column;
          padding: 24px 0;
          &:hover {
            .btn-box {
              top: 0;
            }
          }
          .btn-box {
            display: flex;
            position: absolute;
            top: -24px;
            height: 24px;
            left: 0;
            .el-button {
              background: rgba(0, 0, 0, 0.5);
              color: #fff;
              width: 150px;
              border-radius: 0;
              height: 24px;
              line-height: 24px;
            }
          }
          .shopIcon-box {
            margin-top: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            img {
              height: 40px;
              width: 120px;
            }
          }
          .shoptext-box {
            margin: 20px 0;
            display: flex;
            width: 100%;
            align-items: center;
            justify-content: center;
            h3 {
              font-size: 18px;
              color: #333;
            }
          }
          .rate {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px 10px;
            span {
              color: #666;
              font-size: 14px;
              margin: 0 2px;
            }
          }
          .joinCartBtn {
            position: absolute;
            bottom: 0;
            .el-button {
              background-color: #f9f9f9;
              border-radius: 0;
              width: 301px;
              height: 45px;
              font-size: 16px;
              font-weight: bold;
              &:hover {
                background-color: #244d4d;
                color: #fff;
              }
            }
            .shopListIndex {
              width: 30px;
              height: 30px;
              position: absolute;
              bottom: 50px;
              left: 0px;
              background-color: #f2f2f2;
              display: flex;
              align-items: center;
              justify-content: center;
              color: #333;
            }
          }
        }
      }
    }
  }
}
</style>
